<?php
class transpettycontroller extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->model('pettycash_model');
		$this->load->model('transpetty_model');
		$this->load->library('session');
		$this->load->library('pagination_editable');
		if(($this->session->userdata('fullname') == '') || ($this->session->userdata('fullname') == NULL)){
			redirect('logincontroller');
		}
	}
	
	function index()
	{
		$config = array();
		$config['per_page'] = 10;
		$config['first_tag_open'] = '<li>';
		$config['first_tag_close'] = '</li>';
		$config['curr_class'] = 'active';
		$config['full_tag_open'] = '<ul id="pagination-flickr">';
		$config['full_tag_close'] = '</ul>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['next_link'] = 'Next &gt;&gt;';
		$config['prev_link'] = '&lt;&lt; Previous';
		$config['next_tag_open'] = '<li class="next">';
		$config['next_tag_close'] = '</li>';
		$config['prev_tag_open'] = '<li class="previous-off">';
		$config['prev_tag_close'] = '</li>';
		
		$config["uri_segment"] = 3;
		$config["base_url"] = site_url('transpettycontroller/index');
		$config['total_rows'] = $this->transpetty_model->countGetList();
		
		$this->pagination_editable->initialize($config);
	
		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		      
		if (isset($_POST['btnview'])) 
		{
			if($_POST['kdtransview'] == "0")
			{         
				$dataprofile = array(
					'page_title' => 'Finance > Transaksi Petty Cash',
					'name' => $this->session->userdata('fullname'),
					'kodetrans' => $this->transpetty_model->gettransCode(),
					'masterpetty' => $this->pettycash_model->getList(),
					'transpetty' => $this->transpetty_model->getListlimit($config['per_page'],$page),
					'links' => $this->pagination_editable->create_links(),
					'message' => ''		
				);		
			}
			else if($_POST['kdtransview'] == "1")
			{
				$this->transpetty_model->setKdtransaksi($_POST['transview']);
				$dataprofile = array(
					'page_title' => 'Finance > Transaksi Petty Cash',
					'name' => $this->session->userdata('fullname'),
					'kodetrans' => $this->transpetty_model->gettransCode(),
					'masterpetty' => $this->pettycash_model->getList(),
					'transpetty' => $this->transpetty_model->getkdtranslike(),
					'links' => $this->pagination_editable->create_links(),
					'message' => ''			
				);
			}
			else if($_POST['kdtransview'] == "2")
			{
				$this->transpetty_model->setKodepettycash($_POST['transview']);
				$dataprofile = array(
					'page_title' => 'Finance > Transaksi Petty Cash',
					'name' => $this->session->userdata('fullname'),
					'kodetrans' => $this->transpetty_model->gettransCode(),
					'masterpetty' => $this->pettycash_model->getList(),
					'transpetty' => $this->transpetty_model->getkdpettylike(),
					'links' => $this->pagination_editable->create_links(),
					'message' => ''	
				);
			}
		}
		else
		{
			$dataprofile = array(
					'page_title' => 'Finance > Transaksi Petty Cash',
					'name' => $this->session->userdata('fullname'),
					'kodetrans' => $this->transpetty_model->gettransCode(),
					'masterpetty' => $this->pettycash_model->getList(),
					'transpetty' => $this->transpetty_model->getListlimit($config['per_page'],$page),
					'links' => $this->pagination_editable->create_links(),
					'message' => ''	
			);	
			
		}
		$content = array(
			'content' => 'finance/transaksi/petty_cash/transpettycash'
		);
		$this->template->load('templates/default',$content,$dataprofile);
	}
	
    function gettranspetty() {
		$config = array();
		$config['per_page'] = 10;
		$config['first_tag_open'] = '<li>';
		$config['first_tag_close'] = '</li>';
		$config['curr_class'] = 'active';
		$config['full_tag_open'] = '<ul id="pagination-flickr">';
		$config['full_tag_close'] = '</ul>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['next_link'] = 'Next &gt;&gt;';
		$config['prev_link'] = '&lt;&lt; Previous';
		$config['next_tag_open'] = '<li class="next">';
		$config['next_tag_close'] = '</li>';
		$config['prev_tag_open'] = '<li class="previous-off">';
		$config['prev_tag_close'] = '</li>';
		
		$config["uri_segment"] = 3;
		$config["base_url"] = site_url('transpettycontroller/index');
		$config['total_rows'] = $this->transpetty_model->countGetList();
		
		$this->pagination_editable->initialize($config);
	
		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		
		$idkdtrans = $this->uri->segment(3);
		$this->transpetty_model->setKdtransaksi($idkdtrans);

        $dataprofile = array(
            'page_title' => 'Finance > Transaksi Petty Cash',
			'name' => $this->session->userdata('fullname'),
			'kodetrans' => $this->transpetty_model->gettransCode(),
			'masterpetty' => $this->pettycash_model->getList(),
			'transpetty' => $this->transpetty_model->getListlimit($config['per_page'],$page),
			'links' => $this->pagination_editable->create_links(),
			'message' => ''	
            /*'individulist' => $this->Individu_model->getOneSelect(),
            'wilayahlist' => $this->Wilayah_model->getWilayahList(),
            'status_nikah' => $this->Statusnikah_model->getStatusNikahList(),
            'foto' => $this->Individu_model->get_images(),
            'role' => $this->session->userdata('role')*/
        );
        $content = array(
            'content' => 'finance/transaksi/petty_cash/transpettycash'
        );
        $this->template->load('templates/default', $content, $dataprofile);
    }
	
	function tambahtranspetty()
	 {
		$config = array();
		$config['per_page'] = 10;
		$config['first_tag_open'] = '<li>';
		$config['first_tag_close'] = '</li>';
		$config['curr_class'] = 'active';
		$config['full_tag_open'] = '<ul id="pagination-flickr">';
		$config['full_tag_close'] = '</ul>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['next_link'] = 'Next &gt;&gt;';
		$config['prev_link'] = '&lt;&lt; Previous';
		$config['next_tag_open'] = '<li class="next">';
		$config['next_tag_close'] = '</li>';
		$config['prev_tag_open'] = '<li class="previous-off">';
		$config['prev_tag_close'] = '</li>';
		
		$config["uri_segment"] = 3;
		$config["base_url"] = site_url('transpettycontroller/index');
		$config['total_rows'] = $this->transpetty_model->countGetList();
		
		$this->pagination_editable->initialize($config);
	
		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		
		if (isset($_POST['btnSave'])) 
		{
            if (isset($_POST['keterangan']) &&
					isset($_POST['jmlhbyr']) &&
                    !($_POST['keterangan'] == '') &&
                    !($_POST['jmlhbyr'] == '')) 
			{
				$sisasaldo=0;
				$ambilsaldo=0;
				$jumlahbyr = $_POST['jmlhbyr'];
				
	            $this->transpetty_model->setKodepettycash($_POST['kdpetty']);
				$tglskrg=date('Y-m-d', strtotime('now'));
				$this->transpetty_model->setTanggal($tglskrg);
				$this->transpetty_model->setKeterangan($_POST['keterangan']);
				$this->transpetty_model->setAmount($_POST['jmlhbyr']);
                $this->transpetty_model->setCreatedBy($this->session->userdata('userid'));
			   	$tanggal=date('Y-m-d H:i:s', strtotime('now'));
				$this->transpetty_model->setCreatedOn($tanggal);
				
				$this->transpetty_model->insert();
				
				$this->pettycash_model->setKodepettycash($_POST['kdpetty']);
				$ambilsaldo = $this->pettycash_model->getTotalsaldo();
				$sisasaldo = $ambilsaldo - $jumlahbyr;
				$this->pettycash_model->setsaldo($sisasaldo);
				
				$this->pettycash_model->ubahsaldopetty();
				
                redirect('transpettycontroller');
            } 
			else 
			{
                $dataprofile = array(
					'page_title' => 'Finance > Transaksi Petty Cash',
					'name' => $this->session->userdata('fullname'),
					'kodetrans' => $this->transpetty_model->gettransCode(),
					'masterpetty' => $this->pettycash_model->getList(),
					'message' => '<div style="color:red;font-size:9pt;font-weight:bold;">Keterangan dan jumlah bayar Belum Di Isi </div>',
					'transpetty' => $this->transpetty_model->getListlimit($config['per_page'],$page),
					'links' => $this->pagination_editable->create_links()
                );
                $content = array(
                    'content' => 'finance/transaksi/petty_cash/transpettycash'
                );
                $this->template->load('templates/default', $content, $dataprofile);
            }
        } 
		else 
		{
           $dataprofile = array(
			'page_title' => 'Finance > Transaksi Petty Cash',
			'name' => $this->session->userdata('fullname'),
			'kodetrans' => $this->transpetty_model->gettransCode(),
			'masterpetty' => $this->pettycash_model->getList(),
			'transpetty' => $this->transpetty_model->getListlimit($config['per_page'],$page),
			'links' => $this->pagination_editable->create_links(),
			'message' => ''
			);		
			$content = array(
				'content' => 'finance/transaksi/petty_cash/transpettycash'

			);
			$this->template->load('templates/default',$content,$dataprofile);
        }
    }
	
    function ubahtranspetty()
	{
        $idkdtrans = $this->uri->segment(3);
		$this->transpetty_model->setKodepettycash($idkdtrans);
        if (isset($_POST['btnSave'])) 
		{
			if (isset($_POST['keterangan']) &&
					isset($_POST['jmlhbyr']) &&
                    !($_POST['keterangan'] == '') &&
                    !($_POST['jmlhbyr'] == '')) 
			{
				$sisasaldo=0;
				$ambilsaldo=0;
				$jumlahbyr 			= $_POST['jmlhbyr'];
				$jumlahbyrsblmnya 	= $_POST['jmlhbyrsblm'];
				
				$this->transpetty_model->setKdtransaksi($_POST['kdtrans']);
				$this->transpetty_model->setKeterangan($_POST['keterangan']);
				$this->transpetty_model->setAmount($_POST['jmlhbyr']);
				$this->transpetty_model->setLastUpdateBy($this->session->userdata('userid'));
				$tanggal=date('Y-m-d H:i:s', strtotime('now'));
				$this->transpetty_model->setLastUpdateOn($tanggal);
				
				$this->transpetty_model->ubahtranspetty();
				
				$this->pettycash_model->setKodepettycash($_POST['kdpetty']);
				$ambilsaldo = $this->pettycash_model->getTotalsaldo();
				
				
				$sisasaldo = $ambilsaldo - $jumlahbyr + $jumlahbyrsblmnya;
				$this->pettycash_model->setsaldo($sisasaldo);
				
				$this->pettycash_model->ubahsaldopetty();
				
				redirect('transpettycontroller');
			}
			else
			{
				$dataprofile = array(
					'page_title' => 'Finance > Transaksi Petty Cash',
					'name' => $this->session->userdata('fullname'),
					'message' => '<div style="color:red;font-size:9pt;font-weight:bold;">Keterangan dan jumlah bayar Belum Di Isi </div>',
					'transpetty' => $this->transpetty_model->getkdtrans()			
				);		
				
				$content = array(
					'content' => 'finance/transaksi/petty_cash/edittranspetty'
				);
				$this->template->load('templates/default',$content,$dataprofile);
			}
        } else {
            $this->transpetty_model->setKdtransaksi($idkdtrans);
            $dataprofile = array(
				'page_title' => 'Finance > Transaksi Petty Cash',
				'name' => $this->session->userdata('fullname'),
				'transpetty' => $this->transpetty_model->getkdtrans(),
				'message' => ''		
			);		
			$content = array(
				'content' => 'finance/transaksi/petty_cash/edittranspetty'
			);
			$this->template->load('templates/default',$content,$dataprofile);
        }
    }

    function deletetranspetty() {
        $kdtrans = $this->uri->segment(3);
        $this->transpetty_model->setKdtransaksi($kdtrans);
        $this->transpetty_model->deletetranspetty();
        redirect('transpettycontroller');
    }
}